<template>
  <div class="auto-center">
    <p class="upload-title">文件上传：</p>
    <el-upload
    :action="updownUrl"
    list-type="picture-card"
    :limit="3"
    :file-list="fileList"
    :on-preview="handlePictureCardPreview"
    :on-success="handleSuccess"
    :on-remove="handleRemove">
    <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
    <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "upload",
  props:[
    "value"
  ],
  watch:{
    value(val){
      if(val && val.length==0){
        this.fileList = [];
        this.filesId = [];
      }
    }
  },
  mounted() {
    this.updownUrl = process.env.API_BASH_URL + "sys/upload";
  },
  data() {
    return {
      filesId: [], // 存放上传图片的id
      updownUrl: "", // 上传地址
      dialogImageUrl: "", // 上传图片URL
      dialogVisible: false, //是否显示上传图片
      fileList: [],
      uploadUrl: "" // 上传图片地址
    };
  },
  methods: {
    // 上传文件
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(file, fileList) {
      // 调用文件上传成功时的钩子，把上传文件的id存放到数组里
      this.filesId.push(fileList.uid);
      this.$emit('input', this.filesId);
    }
  }
};
</script>

<style scoped>
.upload-title {
  width: 100%;
  float: left;
  height: 44px;
  line-height: 44px;
  margin-top: 34px;
  color: #606266;
}
</style>


